<?php 
class Admin extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->helper("url");
		$this->load->helper('captcha');
		$this->load->library('session'); //初始化session类 session也可以自动加载
		$this->load->database();  //可以设置自动加载数据库
		$this->load->library('pagination');//装载分页类
		
	}
		
	function index(){ //加载进入到后台主页
		$username=$this->session->userdata('username');
        $yes=$this->session->userdata('yes');
		if(!isset($username)||$yes!='mcx'){
			redirect("admin/login");
			
		} else {
			$this->load->view('admin/index');//加载后台
			//echo "yes you do";
			
		}

	}
	function login(){ 
		//检查用户登录信息  
		//$this->load->helper("url");
		//$this->load->helper('captcha');// 装载验证码类函数
		$vals=array(
		'word'=>rand(10000,99999),
		'img_path'=>'./captcha/',
		'img_url'=>base_url().'/captcha/',
		'img_width'=>'120',
		'img_height'=>'25',
		'expiration'=>600 //过期时间
		);
	    $cap=create_captcha($vals);
		$word=$cap['word'];
		//var_dump($cap);
		$data['cap']=$cap['image'];
		$this->session->set_userdata('word',$word);//session方法
		//redirect('admin1/test');
		$this->load->view('admin/login',$data);	
		
	}
	
	function quit(){
		$this->session->sess_destroy();   //销毁session
		redirect("admin/login");
	}
	function check(){
	    //把数据传输到model中任何关于处理数据的全部都放到model中
		//转义字符
		//验证码的判断
		$trueyanzhengma=$this->session->userdata('word');
		$yanzhengma=$this->input->post('cap');
		if($trueyanzhengma!=$yanzhengma){
		    	redirect('admin/login');
		} else {
			$username=$this->input->post('username');	
			$specialstr=array('/and/','/‘/');
			$replacements=array('_','_');
			$username=preg_replace($specialstr,$replacements,$username);
			$password=md5($this->input->post('password'));
			$this->load->model("adminmodel");
			$result=$this->adminmodel->get_user($username);  //返回的是对象数组
			$i=0;
			foreach($result as $row){
				if($row->username==$username && $row->password==$password )	{
					$aname=$row->username;
					$i=1;
					break;
				}
			}	

			if($i!=1){
					redirect('admin/login');
			}else {
					
					$this->session->set_userdata('username',$aname);
                    $yes="mcx";
                    $this->session->set_userdata('yes',$yes);
					$name=$this->session->userdata('username');
					
					redirect('admin/index');
			}

		}
	}
	
	function top(){
		//$this->load->model("adminmode");
		//$data['top']=$this->adminmodel->top();
		$username=$this->session->userdata('username');
		if(!isset($username)){
		   redirect("admin/login");
		 }
        $data['name']=$this->session->userdata('username');
		
		$this->load->view('admin/top',$data);
	}
	function main(){
		$username=$this->session->userdata('username');
		if(!isset($username)){
		   redirect("admin/login");
		 }
		// echo empty($username);
		// exit;
		$this->load->view('admin/main');
	 
	}
	
	 function menu(){
		$this->load->model('adminmodel');
		$data['category']=$this->adminmodel->get_category();
		$data['cid']=0;
		$data['cid']=$this->uri->segment(3);//后面是加的
		//$this->load->model('adminmodel');
		// $data['top1']=$this->adminmodel->top();
		// $data['top']=$this->adminmodel->menu();

		$this->load->view('admin/menu',$data);
		
	 }
	 
	 /*
      *
	  *文章管理
	  *
	  */
	function  articlelist(){
		$username=$this->session->userdata('username');
		if(!isset($username)){
		   redirect("admin/login");
		 }
		$data['cid']=$this->uri->segment(3);
		$this->load->model("adminmodel");
		$config['base_url']=site_url("admin/articlelist/".$data['cid']);
		$config['total_rows']=$this->adminmodel->get_article_rows($data['cid']);
		$config['num_links']='3';
		$config['uri_segment']='4';
		$config['per_page']='5';
		$config['first_link']='首页';
		$config['last_link']='末页';
		$config['next_link']='下一页';
		$config['prev_link']='上一页';
		$this->pagination->initialize($config);
		$data['links']=$this->pagination->create_links();
		$data['articlelist']=$this->adminmodel->get_article_list($data['cid'],$this->uri->segment(4,0),$config['per_page']);
		$this->load->view("admin/article/articlelist",$data);	
	
		// $data['cid']=$this
		// $perpage=3;//每页显示条数
		// $config['base_url']=site_url('admin/articlelist');
		// $config['total_rows']=$this->admin_model->get_article_rows($data['cid']);//得到总条数;
		// $config['per_page'] = $perpage;
	    // $config['first_link'] = '首页';
	    // $config['next_link'] = '下一页';
	    // $config['prev_link'] = '上一页';
	    // $config['last_link'] = '末页';
        // $config['uri_segment']=3;//分页的数据查询偏移量在哪一段上
		// $this->pagination->initialize($config); //载入配置
		 
	    // $offset=intval($this->uri->segment(3));
	     

        // $data['links']=$this->pagination->create_links();
	    // $this->db->limit($perpage,$offset);
		 
		// $this->load->model('adminmodel','art');
		// $data['article']=$this->art->getarticle();
		// $this->db->limit($perpage,$offset);
		 // 后来增加的 为了显示取消置顶 和 已置顶
		 // $data['a']=$a;
		// $this->load->view('admin/article/articlelist',$data);
	}
	function addarticle(){
		$username=$this->session->userdata('username');
		if(!isset($username)){
		   redirect("admin/login");
		 }
		$data['cid']=$this->uri->segment(3);
		$this->load->view('admin/article/addarticle',$data);
		
		
		
		
		
	}
    function checkaddarticle(){
	
		$username=$this->session->userdata('username');
		if(!isset($username)){
		   redirect("admin/login");
		 }
		$submit=$this->input->post('submit');
		$cid=$this->input->post('cid');
	    if(isset($submit)){
		$this->load->model('adminmodel');
		$content=$this->input->post('content');//通过正则得到图片的路径
		$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
		preg_match_all($pattern,$content,$match);	
        $picture=$match[1][0];
		//$picture=substr($picture,26);
		$url=$picture;
		//$content=$this->input->post('content');//通过正则得到图片的路径
		$a=$this->adminmodel->addarticle($url);
			if($a>0){
				redirect('admin/articlelist/'.$cid);
			}else {
				redirect('admin/addarticle/'.$cid);
			}
			
	    }else {
				redirect('amdin/addarticle/'.$cid);
		} 


	}
	
	/*
	function test6(){
		$username=$this->session->userdata('username');
		if(!isset($username)){
		   redirect("admin/login");
		 }
		//$submit=$this->input->post('submit');
		//$cid=$this->input->post('cid');
	    //if(isset($submit)){
		//$this->load->model('adminmodel');
		$content=$this->input->post('content');//通过正则得到图片的路径
		$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
		preg_match_all($pattern,$content,$match);	
       		 $picture=$match[1][0];
		//$picture=substr($picture,);
		echo $picture;
		exit;
	
			
		/* $a=$this->adminmodel->addarticle();
			if($a>0){
				redirect('admin/articlelist/'.$cid);
			}else {
				redirect('admin/addarticle/'.$cid);
			}
			
	    }else {
				redirect('amdin/addarticle/'.$cid);
		}	 */
			
	//} 
	
	
	
	
	
	
	function editarticle(){ //进入文章编辑页面
		$username=$this->session->userdata('username');
		if(!isset($username)){
		   redirect("admin/login");
		 }
		$id=$this->uri->segment(3);	

	    $this->load->model('adminmodel');
		$result=$this->adminmodel->editarticle($id);  //返回的是对象数组
		$data['article']=$result;
		$this->load->view('admin/article/editarticle',$data);
	
	}
	function updatearticle(){	
		$username=$this->session->userdata('username');
		if(!isset($username)){
		   redirect("admin/login");
		 }
		$submit=$this->input->post('submit');
		$cid=$this->input->post('cid');
	    if(isset($submit)){
		    $this->load->model('adminmodel');
			$a=$this->adminmodel->updatearticle();
			if($a>0){

				redirect('admin/articlelist/'.$cid);  //提高效率和准确率
			}else {
		
				redirect('admin/articlelist/'.$cid);
			}

		} else {
			   redirect('admin/editarticle');
		}
	
	}
	function settop(){
		$username=$this->session->userdata('username');
		if(!isset($username)){
		   redirect("admin/login");
		 }
		$cid=$this->uri->segment(4);
		$this->load->model('adminmodel');
		$a=$this->adminmodel->settop();
		if($a>0){
				redirect('admin/articlelist/'.$cid);
		}else {
		    $data['error']="置顶失败";          //处理发生错误的提示
		    $this->load->view("admin/article/articlelist",$data);
	
			
		}
	
	}
	function cancelsettop(){
		$username=$this->session->userdata('username');
		if(!isset($username)){
		   redirect("admin/login");
		 }
		$cid=$this->uri->segment(4);
		$this->load->model('adminmodel');
		$a=$this->adminmodel->cancelsettop();
		if($a>0){
			redirect('admin/articlelist/'.$cid);
		}else {
		    $data['error']="取消置顶失败";          //处理发生错误的提示
		    $this->load->view("admin/article/articlelist",$data);

		}
	}
	function delarticle(){
		$username=$this->session->userdata('username');
		if(!isset($username)){
		   redirect("admin/login");
		 }
		$cid=$this->uri->segment(4);
		$id=$this->uri->segment(3);
		$this->load->model('adminmodel');
		$a=$this->adminmodel->delarticle($id);
		if($a>0){
				redirect('admin/articlelist/'.$cid);
		}else {
		    $data['error']="删除失败";          //处理发生错误的提示
		    $this->load->view("admin/article/articlelist",$data);
	
			
		}
		
		
	}
	 /*
      *
	  *评论管理
	  *
	  */
	
		function commentlist(){  //利用的是id 代表属于哪一篇文章的
			//echo "hello world";
			$username=$this->session->userdata('username');
			if(!isset($username)){
				redirect("admin/login");
			}
			$id=$this->uri->segment(3);
			$this->load->model('adminmodel');
			$data['id']=$this->uri->segment(3);
			$config['base_url']=site_url("admin/commentlist/".$data['id']);
			$config['total_rows']=$this->adminmodel->get_comment_rows($data['id']);
			$config['num_links']='3';
			$config['uri_segment']='4';
			$config['per_page']='5';
			$config['first_link']='首页';
			$config['last_link']='末页';
			$config['next_link']='下一页';
			$config['prev_link']='上一页';
			$this->pagination->initialize($config);
			$data['links']=$this->pagination->create_links();
			$data['commentlist']=$this->adminmodel->getcomment($data['id'],$this->uri->segment(4,0),$config['per_page']);
			$this->load->view("admin/comment/commentlist",$data);	
		}
		function delcomment(){
			$id=$this->uri->segment(4);
			$this->load->model('adminmodel');
			$a=$this->adminmodel->delcomment();
			if($a>0){
				redirect('admin/commentlist/'.$id);
			}else{
				$data['error']="删除失败";          //处理发生错误的提示
				$this->load->view("admin/comment/commentlist",$data);
			}
		
		}
		
			/*
	 * 
	 * 文件操作
	 * 
	 * 
	 * 
	 * 
	 * 
	 */
	
	function file_list(){
		$data['cid']=$this->uri->segment(3);
		$this->load->model("adminmodel");
		$config['base_url']=site_url("admin/file_list/".$data['cid']);
		$config['total_rows']=$this->adminmodel->get_file_rows($data['cid']);
		$config['num_links']='3';
		$config['uri_segment']='4';
		$config['per_page']='2';
		$config['first_link']='首页';
		$config['last_link']='末页';
		$config['next_link']='下一页';
		$config['prev_link']='上一页';
		$this->pagination->initialize($config);
		$data['file_list']=$this->adminmodel->get_file_list($data['cid'],$this->uri->segment(4,0),$config['per_page']);
		$this->load->view("admin/file/file_list",$data);
	}
	
	function add_file(){
		$data['cid']=$this->uri->segment(3);
		$this->load->view("admin/file/add_file",$data);
	}
	
	function check_add_file(){
		$file_array=array();
		$data['cid']=$this->input->post("cid");
		$config['upload_path']="uploads/files/";
		$config['allowed_types']='doc|txt|wps|docx|pdf|xls|et|ppt|dps|xlsx|pptx|rar|zip';
		$config['max_size']='1000000';
		$config['file_name']=date('Y-m-d-H-i-s',time());
		$this->load->library("upload",$config);
		if(!$this->upload->do_upload()){
			$data["error"]=$this->upload->display_errors();
			$this->load->view("admin/file/add_file",$data);
		}else{
			$data['upload_data']=$this->upload->data();
			$data['cid']=$this->input->post("cid");
			$data['name']=$this->input->post("name");
			$data['time']=$this->input->post("time");
			$data['url']=$config['upload_path'].$config['file_name'].$data['upload_data']['file_ext'];
			$file_array[]=$data['cid'];
			$file_array[]=$data['name'];
			$file_array[]=$data['url'];
			$file_array[]=$data['time'];
			$this->load->model("adminmodel");
			$result=$this->adminmodel->add_file($file_array);
			if($result==true){
				redirect("admin/file_list/".$data['cid']);
			}else{
				$data['error']="上传失败，请重新上传";
				$this->load->view("admin/file/add_file",$data);
			}
		}
	}
	
	function edit_file(){
		$data['id']=$this->uri->segment(3);
		$this->load->model("adminmodel");
		$data['file_content']=$this->adminmodel->get_file_content($data['id']);
		$this->load->view("admin/file/edit_file",$data);
	}
	
	function check_edit_file(){
		$cid=$this->input->post("cid");
		$this->load->model("adminmodel");
		if($_FILES["userfile"]["name"]==null||$_FILES["userfile"]["name"]==" "){
			$result=$this->adminmodel->no_edit_file();
			if($result==true){
				redirect("admin/file_list/".$cid);
			}else{
				$data["error"]="修改失败，请重新修改！";
				$this->load->view("admin/file/edit_file",$data);
			}
		}else{
			$file_array=array();
			$config["upload_path"]="uploads/files/";
			$config["allowed_types"]="doc|txt|wps|docx|pdf|xls|et|ppt|dps|xlsx|pptx|rar|zip";
			$config["max_size"]="1000000";
			$config["file_name"]=date('Y-m-d-H-i-s',time());
			$this->load->library("upload",$config);
			if(!$this->upload->do_upload()){
				$data["error"]=$this->upload->display_errors();
				$this->load->view("admin/file/edit_file",$data);
			}else{
				$data["upload_data"]=$this->upload->data();
				$data["name"]=$this->input->post("name");
				$data["time"]=$this->input->post("time");
				$data["url"]=$config["upload_path"].$config["file_name"].$data["upload_data"]["file_ext"];
				$file_array[]=$data["name"];
				$file_array[]=$data["time"];
				$file_array[]=$data["url"];
				$result=$this->adminmodel->edit_file($file_array);
				if($result==true){
					redirect("admin/file_list/".$cid);
				}else{
					$data["error"]="上传失败，请重新上传！";
					$this->load->view("admin/file/edit_file",$data);
				}
			}
		}
	}
	
	function del_file(){
		$id=$this->uri->segment(3);
		$this->load->model("adminmodel");
		$cid=$this->adminmodel->get_file_cid($id);
		$result=$this->adminmodel->del_file($id);
		if($result==true){
			redirect("admin/file_list/".$cid);
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}



?>